//editor functions

function addSubtitleLine() {

	var subtitleTime = $('#newSubtitleTime').val();
	var subtitleValue = $('#newSubtitleValue').val();

	if (subtitleTime && subtitleValue) {
		
		
		var captionDescriptor ={ time:subtitleTime, duration:"4", text:subtitleTime};
		captionService.addCaptionDescriptor(captionDescriptor);
		
		var newSubtitleLineHtml = '<tr><td>' + subtitleTime + '</td><td><input value="4" maxlength="1" size="1"/></td><td><textarea>' + subtitleValue + '</textarea></td><td><button onclick="removeSubtitleLine(event);">X</button></td></tr>';
		$('#subtitleTableBody').append(newSubtitleLineHtml);

		var newSubtitleTableRow = $('#subtitleTableBody > tr:last-child').hide();
		// let the plugin know that we made a update and sort the table using the time 
		$("#subtitleTable").trigger("update").trigger("sorton",[[[0,0]]]);
        //fade the new row.
		newSubtitleTableRow.fadeIn(1500);
		//scroll to the new row
		$('#subtitleTableContainer').scrollTo(newSubtitleTableRow,500);
		
		
		//clear time and value
		//todo $('#newSubtitleTime').empty();
		$('#newSubtitleValue').empty();
	}
}


function removeSubtitleLine(e)
{
	var subtitleRow = e.target.parentNode.parentNode;//better jquery way?
	$(subtitleRow).fadeOut('slow', function() {
		$(this).remove();
	});
}

function onYoutubePlayerStateChange(state) {
	
	if(state == 1) {
//		onPlayVideo();
	}
	
	else if(state == 2) {
		onPauseVideo();
	}
}

function onPauseVideo() {
	$('#newSubtitleTime').attr("value", getCurrentTime());	
}

function onSeek() {
	// ummm better way to do this
	seekTo($('#newSubtitleTime').val())
	pause();
}